// 钱包相关API

/**
 * 获取用户代币余额
 * @returns {Promise} 返回包含代币余额的Promise
 */
export const getTokenBalance = () => {
  // 后端集成时使用axios请求
  return new Promise((resolve) => {
    setTimeout(() => {
      const tokenBalance = localStorage.getItem('tokenBalance') || 0
      resolve({
        code: 200,
        data: {
          balance: Number(tokenBalance)
        },
        message: '获取成功'
      })
    }, 300)
  })
}

/**
 * 充值代币
 * @param {number} amount - 充值金额
 * @returns {Promise} 充值结果
 */
export const depositTokens = (amount) => {
  // 后端集成时使用axios请求
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      try {
        if (!amount || isNaN(Number(amount)) || Number(amount) <= 0) {
          reject({
            code: 400,
            message: '无效的充值金额'
          })
          return
        }
        
        // 模拟支付流程
        // 实际项目中应对接支付系统
        
        resolve({
          code: 200,
          data: {
            transactionId: Date.now().toString(),
            amount: Number(amount),
            timestamp: new Date().toISOString()
          },
          message: '充值成功'
        })
      } catch (error) {
        reject({
          code: 500,
          message: error.message || '充值失败'
        })
      }
    }, 600)
  })
}

/**
 * 提现代币
 * @param {number} amount - 提现金额
 * @param {object} bankInfo - 银行账户信息
 * @returns {Promise} 提现结果
 */
export const withdrawTokens = (amount, bankInfo) => {
  // 后端集成时使用axios请求
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      try {
        if (!amount || isNaN(Number(amount)) || Number(amount) <= 0) {
          reject({
            code: 400,
            message: '无效的提现金额'
          })
          return
        }
        
        if (!bankInfo || !bankInfo.name || !bankInfo.account) {
          reject({
            code: 400,
            message: '请提供有效的银行账户信息'
          })
          return
        }
        
        // 模拟提现流程
        // 实际项目中应对接银行转账系统
        
        resolve({
          code: 200,
          data: {
            transactionId: Date.now().toString(),
            amount: Number(amount),
            bankInfo,
            timestamp: new Date().toISOString(),
            estimatedArrivalTime: '1-3个工作日'
          },
          message: '提现申请已提交，资金将在1-3个工作日内到账'
        })
      } catch (error) {
        reject({
          code: 500,
          message: error.message || '提现失败'
        })
      }
    }, 800)
  })
}

/**
 * 获取交易记录
 * @param {object} params - 查询参数
 * @param {number} params.page - 页码
 * @param {number} params.limit - 每页条数
 * @param {string} params.type - 交易类型：deposit(充值)、withdraw(提现)
 * @returns {Promise} 交易记录
 */
export const getTransactionHistory = (params = {}) => {
  // 后端集成时使用axios请求
  return new Promise((resolve) => {
    setTimeout(() => {
      try {
        const history = JSON.parse(localStorage.getItem('transactionHistory') || '[]')
        let filteredHistory = [...history]
        
        // 按类型筛选
        if (params.type) {
          filteredHistory = filteredHistory.filter(item => item.type === params.type)
        }
        
        // 排序：最新的交易排在前面
        filteredHistory.sort((a, b) => new Date(b.date) - new Date(a.date))
        
        // 分页
        const page = params.page || 1
        const limit = params.limit || 10
        const start = (page - 1) * limit
        const end = start + limit
        const paginatedHistory = filteredHistory.slice(start, end)
        
        resolve({
          code: 200,
          data: {
            list: paginatedHistory,
            total: filteredHistory.length,
            page,
            limit
          },
          message: '获取成功'
        })
      } catch (error) {
        resolve({
          code: 500,
          data: {
            list: [],
            total: 0
          },
          message: error.message || '获取交易记录失败'
        })
      }
    }, 500)
  })
}

/**
 * 绑定银行账户
 * @param {object} bankInfo - 银行账户信息
 * @param {string} bankInfo.name - 银行名称
 * @param {string} bankInfo.account - 银行账号
 * @returns {Promise} 绑定结果
 */
export const bindBankAccount = (bankInfo) => {
  // 后端集成时使用axios请求
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      try {
        if (!bankInfo || !bankInfo.name || !bankInfo.account) {
          reject({
            code: 400,
            message: '请提供有效的银行账户信息'
          })
          return
        }
        
        resolve({
          code: 200,
          data: {
            bankInfo,
            bindTime: new Date().toISOString()
          },
          message: '银行账户绑定成功'
        })
      } catch (error) {
        reject({
          code: 500,
          message: error.message || '银行账户绑定失败'
        })
      }
    }, 600)
  })
} 